using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RedPointManager : MonoBehaviour
{
    [SerializeField] Text txtRoot;
    [SerializeField] Text txtModelA;
    [SerializeField] Text txtModelA_1;
    [SerializeField] Text txtModelA_2;
    [SerializeField] Text txtModelB;
    [SerializeField] Text txtModelB_1;
    [SerializeField] Text txtModelB_2;


    [SerializeField] private Button btnModelA_1;
    [SerializeField] private Button btnModelA_2;
    [SerializeField] private Button btnModelB_1;
    [SerializeField] private Button btnModelB_2;

    RedPointTree redTree;
    public void Start()
    {
        RegisterClickEvents();

        CreateTree();
        SetCallBack();
        AddData();
        //UpdateRoot(redTree.GetRedPointCount(NodeNames.Root));
    }

    private void CreateTree() {
        redTree = new RedPointTree();
        for (int i = 0; i < NodeNames.NodeList.Count; i++) {
            redTree.InsertNode(NodeNames.NodeList[i]);
        }
    }

    private void SetCallBack() {
        redTree.SetCallBack(NodeNames.Root,         UpdateRoot);
        redTree.SetCallBack(NodeNames.ModelA,       UpdateModelA);
        redTree.SetCallBack(NodeNames.ModelA_Sub_1, UpdateModelA_1);
        redTree.SetCallBack(NodeNames.ModelA_Sub_2, UpdateModelA_2);

        redTree.SetCallBack(NodeNames.ModelB,       UpdateModelB);
        redTree.SetCallBack(NodeNames.ModelB_Sub_1, UpdateModelB_1);
        redTree.SetCallBack(NodeNames.ModelB_Sub_2, UpdateModelB_2);
    }

    private void AddData() {
        redTree.ChangeRedPointCount(NodeNames.ModelA_Sub_1, 1);
        redTree.ChangeRedPointCount(NodeNames.ModelA_Sub_2, 1);
        redTree.ChangeRedPointCount(NodeNames.ModelB_Sub_1, 1);
        redTree.ChangeRedPointCount(NodeNames.ModelB_Sub_2, 1);
    }



    private void UpdateRoot(int redCount) {
        ShowText(txtRoot,redCount);
    }
    private void UpdateModelA(int redCount)
    {
        ShowText(txtModelA, redCount);
    }
    private void UpdateModelA_1(int redCount)
    {
        ShowText(txtModelA_1, redCount);
    }
    private void UpdateModelA_2(int redCount)
    {
        ShowText(txtModelA_2, redCount);
    }
    private void UpdateModelB(int redCount)
    {
        ShowText(txtModelB, redCount);
    }
    private void UpdateModelB_1(int redCount)
    {
        ShowText(txtModelB_1, redCount);
    }
    private void UpdateModelB_2(int redCount)
    {
        ShowText(txtModelB_2, redCount);
    }
    private void ShowText(Text txt,int count) {
        txt.text = count.ToString();
    }



    private void RegisterClickEvents() {
        btnModelA_1.onClick.AddListener(A_1Click);
        btnModelA_2.onClick.AddListener(A_2Click);
        btnModelB_1.onClick.AddListener(B_1Click);
        btnModelB_2.onClick.AddListener(B_2Click);
    }
    private void A_1Click() {
        redTree.ChangeRedPointCount(NodeNames.ModelA_Sub_1, -1);
    }
    private void A_2Click()
    {
        redTree.ChangeRedPointCount(NodeNames.ModelA_Sub_2, -1);
    }
    private void B_1Click()
    {
        redTree.ChangeRedPointCount(NodeNames.ModelB_Sub_1, -1);
    }
    private void B_2Click()
    {
        redTree.ChangeRedPointCount(NodeNames.ModelB_Sub_2, -1);
    }
}

public class NodeNames
{
    public const string Root         = "Root";

    public const string ModelA       = "Root|ModelA";
    public const string ModelA_Sub_1 = "Root|ModelA|ModelA_Sub_1";
    public const string ModelA_Sub_2 = "Root|ModelA|ModelA_Sub_2";

    public const string ModelB = "Root|ModelB";
    public const string ModelB_Sub_1 = "Root|ModelB|ModelB_Sub_1";
    public const string ModelB_Sub_2 = "Root|ModelB|ModelB_Sub_2";

    public static List<string> NodeList = new List<string>() {
        Root,ModelA,ModelA_Sub_1,ModelA_Sub_2,ModelB,ModelB_Sub_1,ModelB_Sub_2
    };
}
